<template>
	<div class="current-page">
		<MaterialTitleStatistics />
		<div class="search-item">
			<div class="search-item-material search-item-common">
				<div class="search-item-text">素材类型：</div>
				<n-select
					v-model:value="searchData.resourceType"
					class="search-item-material-select"
					placeholder="全部"
					:options="options"
					@update:value="updateResourceType"
				/>
			</div>
			<div class="search-item-product search-item-common">
				<div class="search-item-text">商品信息：</div>
				<n-input
					v-model:value="searchData.keyWord"
					class="search-item-product-input"
					type="text"
					placeholder="请输入产品名称/产品ID"
					@keyup.enter="updateResourceType"
					@blur="updateResourceType"
				>
					<template #suffix>
						<n-icon style="cursor: pointer" @click="updateResourceType">
							<Search />
						</n-icon>
					</template>
				</n-input>
			</div>
			<div class="search-item-creator search-item-common">
				<div class="search-item-text">创作者信息：</div>
				<n-input
					v-model:value="searchData.create"
					class="search-item-creator-input"
					type="text"
					placeholder="请输入名称/电话"
					@keyup.enter="updateResourceType"
					@blur="updateResourceType"
				/>
			</div>
			<div class="search-item-reset">
				<n-button class="search-item-reset-button" text-color="#333" @click="resetSelect">
					重置
				</n-button>
			</div>
		</div>
		<MaterialList :list="materialData" />
		<div class="page-item">
			<n-pagination
				v-model:page="searchData.pageIndex"
				class="material-pagination"
				:item-count="total"
				:page-size="searchData.pageSize"
				show-quick-jumper
				@update:page="getMaterialPage"
			/>
		</div>
	</div>
</template>

<script>
import MaterialTitleStatistics from '@/pages/material/components/MaterialTitleStatistics.vue';
import MaterialList from '@/pages/material/components/MaterialList.vue';
import { Search } from '@vicons/ionicons5';
import { getMaterialPage, getMaterialType } from '../../../api/material';
export default {
	name: 'MaterialLibrary',
	components: { MaterialTitleStatistics, MaterialList, Search },
	data() {
		return {
			options: [],
			materialData: [],
			materialStatisticsData: [],
			searchData: {
				resourceType: 0,
				keyWord: '',
				create: '',
				pageIndex: 1,
				pageSize: 14
			},
			total: 0
		};
	},
	created() {
		this.getQuery();
		this.getMaterialType();
		this.getMaterialPage();
	},
	methods: {
		getQuery() {
			console.log(this.$route.query);
			if (this.$route.query.create) {
				this.searchData.create = this.$route.query.create;
			} else if (this.$route.query.keyWord) {
				this.searchData.keyWord = this.$route.query.keyWord;
			}
		},
		resetSelect() {
			this.searchData = {
				resourceType: 0,
				keyWord: '',
				create: '',
				pageIndex: 1,
				pageSize: 14
			};
			this.getMaterialPage();
		},
		updateResourceType() {
			this.searchData.pageIndex = 1;
			this.getMaterialPage();
		},
		getMaterialType() {
			getMaterialType().then((res) => {
				const { data = [] } = res;
				data.forEach((item) => {
					item.label = item.name;
				});
				this.options = data;
			});
		},
		getMaterialPage() {
			getMaterialPage(this.searchData).then((res) => {
				const {
					code,
					data: { data, rowCount }
				} = res;
				if (code === 200) {
					this.materialData = data;
					this.total = rowCount;
				}
			});
		}
	}
};
</script>

<style lang="less" scoped>
.current-page {
	height: 100%;
	position: relative;
	background: white;
}
.title-statistics {
	display: flex;
	align-items: center;
	margin-top: 10px;
	padding: 0 10px;
}
.search-item {
	//max-width: 1900px;
	min-height: 50px;
	background: #f5f5f5;
	border-radius: 4px;
	border: 1px solid #f0f0f0;
	margin: 10px 10px 0 10px;
	display: flex;
	align-items: center;
	flex-wrap: wrap;
	position: relative;
	&-common {
		display: flex;
	}
	&-material {
		margin-left: 12px;
		&-select {
			width: 120px;
			height: 32px;
			border-radius: 4px;
		}
	}
	&-product {
		margin-left: 20px;
		&-input {
			width: 204px;
			height: 32px;
			border-radius: 4px;
		}
	}
	&-creator {
		margin-left: 20px;
		&-input {
			width: 181px;
			height: 32px;
			border-radius: 4px;
		}
	}
	&-text {
		font-size: 14px;
		font-weight: 500;
		color: #333333;
		line-height: 32px;
	}
	&-reset {
		position: absolute;
		right: 10px;

		&-button {
			width: 80px;
			height: 32px;
			border-radius: 4px;
			font-size: 13px;
			font-weight: 400;
			background-color: rgba(0, 0, 0, 0.1);
		}
	}
}
.page-item {
	box-shadow: 0px 1px 2px -2px rgba(0, 0, 0, 0.16), 0px 3px 6px 0px rgba(0, 0, 0, 0.12),
		0px 5px 12px 4px rgba(0, 0, 0, 0.09);
	background-color: white;
	padding: 10px;
	position: fixed;
	right: 0;
	left: 0;
	bottom: 0px;
	height: 52px;
	text-align: right;
	z-index: 10;

	.material-pagination {
		position: absolute;
		right: 10px;
	}
}
</style>
